module.exports = function (app, api_prefix = '/api/file', upload_folder) {
    const fs = require("fs");

    const multer = require('multer'); // 支持 multipart/form-data 类型
    const upload = multer();
    // cgi, common gateway interface 通用网关接口
    // api, application programming  interface 应用程序接口
    app.use(`${api_prefix}/:purpose`, upload.single("file"), function (req, res) {
        var params = req.method == "GET" ? req.query : req.body;
        var headers = req.headers;
        switch (req.params.purpose) {
            case 'saveproduct':
                fs.writeFile(path.join(__dirname, "/upload/productlist.json"), JSON.stringify(params), (err) => {
                    if (err) throw err;
                    res.send(JSON.stringify({
                        code: 0,
                        data: params
                    }));
                    console.log('It\'s saved!');
                });
                break;
                //确认文件进度
            case 'checkfile':
                if (headers.filename && headers.position && headers.hash) {
                    var filepath = upload_folder + "/" + headers.filename;
                    fs.access(filepath, (err) => {
                        if (err) {
                            res.send(result(0, {
                                position: 0,
                                hash: headers.hash
                            }))
                        } else {
                            fs.stat(filepath, (err, stats) => {
                                if (err) {
                                    res.send(result(-1, null, err.message));
                                } else {
                                    res.send(result(0, {
                                        position: stats.size
                                    }));
                                }
                            });
                        }
                    });
                } else {
                    res.send(result(-1, params, 'Unavailable file!'))
                }
                break;
            case 'upload': {
                if (req.file) {
                    var filepath = upload_folder + "/" + req.file.originalname;
                    fs.writeFileSync(filepath, req.file.buffer);
                    res.send(result(0));
                } else {
                    res.send(result(-1, params, 'Unavailable file!'));
                }
            }
            case 'writefile': {
                if (headers.filename && headers.position && headers.hash) {
                    var filepath = upload_folder + "/" + headers.filename;
                    var writeStream = fs.createWriteStream(filepath, {
                        flags: 'a+'
                    });
                    var writeError = false;
                    writeStream.on('error', (err) => {
                        writeError = true;
                        res.send(result(-1, null, err.message));
                    });
                    writeStream.on('finish', (err) => {
                        if (writeError) {
                            return;
                        }
                        fs.stat(filepath, (err, stats) => {
                            if (err) {
                                res.send(result(-1, null, err.message));
                            } else {
                                res.send(result(0, {
                                    position: stats.size
                                }));
                            }
                        });
                    });
                    req.on('data', (data) => {
                        writeStream.write(data);
                    });
                    req.on('end', (data) => {
                        writeStream.end();
                    });
                } else {
                    res.send(result(-1, headers, 'Unavailable file!'));
                }
                break;
            }
            default:
                res.send(result(404, null, 'Illigel request!'))
                break;
        }
    });

    function result(code, data, msg) {
        return JSON.stringify({
            code: code,
            data: data,
            msg: msg
        });
    }
}